package config;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * @author: XJK
 */
public class MyCustomRealm1 extends AuthorizingRealm {


    // 获取身份验证信息(认证)
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        String principal = (String) token.getPrincipal();
        System.out.println(principal);
        // 根据用户名验证身份信息
        if("xjk".equals(principal)){
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(principal,"password",this.getName());
            return simpleAuthenticationInfo;
        }

        return null;
    }

    // 获取授权信息
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        // 新创建一个SimpleAuthorizationInfo权限对象
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

        // 将"admin"角色和"user"角色信息赋值给权限对象
        simpleAuthorizationInfo.addRole("admin");
        simpleAuthorizationInfo.addRole("user");


        // 将"writer:write:XJK"和"car:drive"权限信息赋值个权限对象 基于String的许可检查  *号通配
        simpleAuthorizationInfo.addStringPermission("writer:write:*");
        simpleAuthorizationInfo.addStringPermission("root:*:*");
        simpleAuthorizationInfo.addStringPermission("car:drive");

        return simpleAuthorizationInfo;
    }
}
